//
//  MYXInvitationCodeViewController.m
//  meiyinxiang
//
//  Created by 陈李飞 on 2017/11/1.
//  Copyright © 2017年 hbjApple. All rights reserved.
//我的邀请码

#import "MYXInvitationCodeViewController.h"
#import "MYXcodeView.h"
#import "MYXMemberMdel.h"
#import "userinfoModel.h"
@interface MYXInvitationCodeViewController ()
@property (nonatomic, strong) UIView *backgroundView;
@property (nonatomic, strong) MYXcodeView *codeView;
@property (nonatomic, strong)userinfoModel *userinfoMode;
@end

@implementation MYXInvitationCodeViewController


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.view.backgroundColor =[UIColor whiteColor];
    [self.view addSubview:self.backgroundView];
    
    self.codeView=[[MYXcodeView alloc]init];
    self.codeView.frame = CGRectMake(10, 40+SafeAreaTopHeight, WIDTH-20, HEIGHT-120-SafeAreaTopHeight);
    self.codeView.backgroundColor =[UIColor whiteColor];
    [self.view addSubview:self.codeView];
    [self.codeView.iconView sd_setImageWithURL:[NSURL URLWithString:self.userinfoMode.image] placeholderImage:[UIImage imageNamed:@"AppIcon"]];
    self.codeView.nameView.text =self.userinfoMode.nickname;
    self.codeView.contentLable.text =self.userinfoMode.signature;
    [self.codeView.imageView sd_setImageWithURL:[NSURL URLWithString:self.userinfoMode.image] placeholderImage:[UIImage imageNamed:@"AppIcon"]];
     [self initErCodeWithString:[NSString stringWithFormat:@"%@",[MYY_User readUserFromFile].token]];
}
- (UIView *)backgroundView
{
    if (_backgroundView == nil) {
        _backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
        _backgroundView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];
    }
    return _backgroundView;
}

-(void)setMYXMemberMd:(MYXMemberMdel *)MYXMemberMd{
    _MYXMemberMd = MYXMemberMd;
    self.userinfoMode =[[userinfoModel alloc]init];
    self.userinfoMode = MYXMemberMd.userinfo;
    
}



-(void)initErCodeWithString:(NSString *)dataString{
    
    // 1. 实例化二维码滤镜
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    // 2. 恢复滤镜的默认属性
    [filter setDefaults];
    
    // 3. 将字符串转换成NSData
    NSData *data = [dataString dataUsingEncoding:NSUTF8StringEncoding];
    
    // 4. 通过KVO设置滤镜inputMessage数据
    [filter setValue:data forKey:@"inputMessage"];
    
    // 5. 获得滤镜输出的图像
    CIImage *outputImage = [filter outputImage];
   
    
    // 6. 将CIImage转换成UIImage，并放大显示
    
    self.codeView.codeView.image = [self createNonInterpolatedUIImageFormCIImage:outputImage withSize:WIDTH-20];
    
    
}


/**
 * 根据CIImage生成指定大小的UIImage
 *
 * @param image CIImage
 * @param size 图片宽度
 */
- (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size
{
    CGRect extent = CGRectIntegral(image.extent);
    CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
    // 1.创建bitmap;
    size_t width = CGRectGetWidth(extent) * scale;
    size_t height = CGRectGetHeight(extent) * scale;
    CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
    CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
    CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
    CGContextScaleCTM(bitmapRef, scale, scale);
    CGContextDrawImage(bitmapRef, extent, bitmapImage);
    // 2.保存bitmap到图片
    CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
    CGContextRelease(bitmapRef);
    CGImageRelease(bitmapImage);
    return [UIImage imageWithCGImage:scaledImage];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
